﻿Imports System.IO
Imports AutoCAD

Public Class form_工件工具
    ' 服务
    Private cadApplicationService As ICadApplicationService = New CadApplicationServiceImpl
    Private cadPartService As ICadPartService = New CadPartServiceImpl
    Private cadEntityDataService As ICadEntityDataService = New CadEntityDataServiceImpl

    Private doc As AcadDocument
    Private fileName As String
    Private data As New DataTable
    Private highlightEntity As AcadEntity ' 高亮图元



    Private Function getXmlFileName(ByRef doc As AcadDocument) As String
        Return $"{doc.FullName}.xml"
    End Function


    Private Function windowName()
        Return $"工件工具【{fileName}】"
    End Function

    Private Sub LoadXml()
        If File.Exists(fileName) Then
            data = New DataTable
            data.ReadXml(fileName)
        Else
            data = BuildDataTable()
            data.TableName = "parts"
            SaveXml()
        End If
        With dgv
            .DataSource = data
            .AutoResizeColumns()
        End With
        Me.Text = $"{windowName}"
    End Sub

    Private Sub SaveXml()
        data.WriteXml(fileName, XmlWriteMode.WriteSchema)
        Me.Text = $"{windowName}"
    End Sub

    Private Function BuildDataTable() As DataTable
        Return cadEntityDataService.BuildDataTable_PartData
    End Function
    Private Sub btn_连接xml_Click(sender As Object, e As EventArgs) Handles btn_连接xml.Click
        doc = cadApplicationService.GetActiveDocument
        fileName = getXmlFileName(doc)
        Me.Text = windowName
        LoadXml()
    End Sub

    Private Sub btn_保存_Click(sender As Object, e As EventArgs) Handles btn_保存.Click
        SaveXml()
    End Sub

    Private Sub btn_追加_Click(sender As Object, e As EventArgs) Handles btn_追加.Click
        Dim ss As AcadSelectionSet = doc.PickfirstSelectionSet
        cadEntityDataService.FillPartDataTable(data, ss)
    End Sub

    Private Sub btn_删除记录_Click(sender As Object, e As EventArgs) Handles btn_删除记录.Click
        Dim dvr As DataRowView
        For Each dgvr As DataGridViewRow In dgv.SelectedRows
            dvr = dgvr.DataBoundItem
            data.Rows.Remove(dvr.Row)
        Next
    End Sub

    Private Sub 高亮显示图形(ByRef handle As String)
        If Not highlightEntity Is Nothing Then highlightEntity.Highlight(False)
        highlightEntity = doc.HandleToObject(handle)
        highlightEntity.Highlight(True)
    End Sub

    Private Sub 缩放到图形(ByRef handle As String)
        Dim ent As AcadEntity = doc.HandleToObject(handle)
        Dim minp, maxp

        'Dim centroid() = cadEntityDataService.GetCentroid(ent)
        'doc.Application.ZoomCenter(centroid, 1000)

        Dim sc As Integer = 500
        ent.GetBoundingBox(minp, maxp)
        minp(0) -= sc
        minp(1) -= sc
        minp(2) -= sc

        maxp(0) += sc
        maxp(1) += sc
        maxp(2) += sc

        doc.Application.ZoomWindow(minp, maxp)

    End Sub


    Private Sub 刷新尺寸()
        If dgv.DataSource Is Nothing Or dgv.CurrentCell Is Nothing Then Exit Sub
        Dim dr As DataRow = dgv.Rows(dgv.CurrentCell.RowIndex).DataBoundItem.Row

        Dim ent As AcadEntity = doc.HandleToObject(dr("handle"))
        Dim size() As Double = cadEntityDataService.GetPartSize(ent)
        dr("sizeX") = size(0)
        dr("sizeY") = size(1)
        dr("sizeZ") = size(2)
    End Sub

    Private Sub dgv_CellDoubleClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgv.CellDoubleClick
        Dim handle As String = dgv.Rows(e.RowIndex).Cells("handle").Value
        缩放到图形(handle)
        高亮显示图形(handle)
    End Sub

    Private Sub btn_删除垃圾_Click(sender As Object, e As EventArgs) Handles btn_删除垃圾.Click
        doc.PurgeAll()
    End Sub

    Private Sub form_工件工具_PreviewKeyDown(sender As Object, e As PreviewKeyDownEventArgs) Handles Me.PreviewKeyDown
        If e.Control And e.KeyCode = Keys.S Then
            If Not dgv.DataSource Is Nothing Then
                SaveXml()
            End If
        End If
    End Sub


    Private Sub dgv_KeyDown(sender As Object, e As KeyEventArgs) Handles dgv.KeyDown
        If e.Control And e.KeyCode = Keys.S Then
            If Not dgv.DataSource Is Nothing Then
                SaveXml()
            End If
        End If
    End Sub

    Private Sub btn_刷新尺寸_Click(sender As Object, e As EventArgs) Handles btn_刷新尺寸.Click
        刷新尺寸()
    End Sub

    Private Sub btn_批量修改_Click(sender As Object, e As EventArgs) Handles btn_批量修改.Click
        If dgv.DataSource Is Nothing Then Exit Sub
        If dgv.CurrentCell Is Nothing Then Exit Sub
        Dim str As String = InputBox("输入需要修改的内容:", "批量修改")
        If str = "" Then Exit Sub
        For Each cell As DataGridViewCell In dgv.SelectedCells
            cell.Value = str
        Next
        Me.Text = $"{windowName}*"
    End Sub


    Private Sub dgv_CellEndEdit(sender As Object, e As DataGridViewCellEventArgs) Handles dgv.CellEndEdit
        Me.Text = $"{windowName}*"
    End Sub

    Private Sub dgv_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgv.CellContentClick

    End Sub

    Private Sub dgv_CellBeginEdit(sender As Object, e As DataGridViewCellCancelEventArgs) Handles dgv.CellBeginEdit
        Dim handle As String = dgv.Rows(e.RowIndex).Cells("handle").Value
        缩放到图形(handle)
        高亮显示图形(handle)
    End Sub

    Private Sub btn_板件转工件_Click(sender As Object, e As EventArgs) Handles btn_板件转工件.Click
        doc = cadApplicationService.GetActiveDocument
        Dim ents As IEnumerable = doc.PickfirstSelectionSet
        cadPartService.TranslateToPart(doc, ents)
    End Sub

    Private Sub btn_平铺工件副本_Click(sender As Object, e As EventArgs) Handles btn_平铺工件副本.Click
        doc = cadApplicationService.GetActiveDocument
        Dim ents As IEnumerable = doc.PickfirstSelectionSet
        cadPartService.CopyAndExpandPart(doc, ents)
    End Sub

    Private Sub btn_建三视图_Click(sender As Object, e As EventArgs) Handles btn_建三视图.Click
        doc = cadApplicationService.GetActiveDocument
        Dim ents As IEnumerable = doc.PickfirstSelectionSet
        cadPartService.CreateThreeViews(doc, ents)
    End Sub

    Private Sub dgv_DataSourceChanged(sender As Object, e As EventArgs) Handles dgv.DataSourceChanged

    End Sub

    Private Sub tb_筛选_Click(sender As Object, e As EventArgs) Handles tb_筛选.Click

    End Sub

    Private Sub tb_筛选_TextChanged(sender As Object, e As EventArgs) Handles tb_筛选.TextChanged

    End Sub

    Private Sub tb_筛选_LostFocus(sender As Object, e As EventArgs) Handles tb_筛选.LostFocus
        Try
            data.DefaultView.RowFilter = tb_筛选.Text
        Catch ex As Exception

        End Try

    End Sub

    Private Sub dgv_BindingContextChanged(sender As Object, e As EventArgs) Handles dgv.BindingContextChanged

    End Sub

    Private Sub dgv_RowStateChanged(sender As Object, e As DataGridViewRowStateChangedEventArgs) Handles dgv.RowStateChanged
        lbl_记录数量.Text = dgv.RowCount
    End Sub
End Class